package com.zcl.day13_demo7.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zcl.day13_demo7.R;
import com.zcl.day13_demo7.bean.InFoBean;

import java.util.ArrayList;
import java.util.List;

public class RlvAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<InFoBean.ResultsBean> beans = new ArrayList<>();
    public RlvAdapter(Context context) {
        this.context = context;
    }
    public void addData(List<InFoBean.ResultsBean> beans){
        this.beans.addAll(beans);
        notifyDataSetChanged();

    }


    private static final int TIME_ONE = 0;
    private static final int TIME_TWO = 1;
    private static final int TIME_THREE = 2;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == TIME_ONE) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_one, viewGroup, false);
            return new ViewHolder(inflate);
        } else if (i == TIME_TWO) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_two, viewGroup, false);
            return new ViewHolderTwo(inflate);
        } else if (i == TIME_THREE) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_three, viewGroup, false);
            return new ViewHolderThree(inflate);
        }
        return null;

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        InFoBean.ResultsBean resultsBean = beans.get(i);
        int itemViewType = getItemViewType(i);
        if(itemViewType==0){
            ViewHolder viewHolder1=(ViewHolder) viewHolder;
            Glide.with(context).load(resultsBean.getUrl_1()).into(viewHolder1.image);
        }else if(itemViewType==1){
            ViewHolderTwo viewHolder2=(ViewHolderTwo) viewHolder;
            Glide.with(context).load(resultsBean.getUrl_1()).into(viewHolder2.image1);
            Glide.with(context).load(resultsBean.getUrl_1()).into(viewHolder2.image2);
            Glide.with(context).load(resultsBean.getUrl_1()).into(viewHolder2.image3);
        }else if(itemViewType==2){
            ViewHolderThree viewHolder3=(ViewHolderThree) viewHolder;
            Glide.with(context).load(resultsBean.getUrl_2()).into(viewHolder3.image_three);
        }
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 3 == 0) {
            return TIME_ONE;
        } else if (position % 3 == 1) {
            return TIME_TWO;
        } else if (position % 3 == 2) {
            return TIME_THREE;
        }
        return super.getItemViewType(position);
    }




    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView image;
        public TextView text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.image = (ImageView) itemView.findViewById(R.id.image);
            this.text = (TextView) itemView.findViewById(R.id.text);
        }
    }

    public  class ViewHolderTwo extends RecyclerView.ViewHolder {
        public ImageView image1;
        public ImageView image2;
        public ImageView image3;

        public ViewHolderTwo(@NonNull View itemView) {
            super(itemView);
            this.image1 = (ImageView) itemView.findViewById(R.id.image1);
            this.image2 = (ImageView) itemView.findViewById(R.id.image2);
            this.image3 = (ImageView) itemView.findViewById(R.id.image3);
        }
    }
    public class ViewHolderThree extends RecyclerView.ViewHolder {
        public ImageView image_three;

        public ViewHolderThree(@NonNull View itemView) {
            super(itemView);
            this.image_three = (ImageView) itemView.findViewById(R.id.image_three);
        }
    }
}
